package com.example.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @Classname FeignClientInterceptor
 * @Description TODO
 * @Date 2022/3/22 23:45
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Component
public class FeignClientInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        //拦截器
        //使用RequestContextHolder工具获取request相关变量
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

            if (attributes != null){

//            取出request
                HttpServletRequest request = attributes.getRequest();

//                取出所有请求头
                Enumeration<String> headerNames = request.getHeaderNames();
                while (headerNames.hasMoreElements()){
                    String name = headerNames.nextElement();   // key
                    String value = request.getHeader(name);   //value
                    System.out.println("name="+name+"values="+value);
                    if(name.equals("authorization")){    //auoth2 认证请求头
                        System.out.println("11.name="+name+"values="+value);
                        requestTemplate.header(name, value);
                    }
                }
            }
        }catch (Exception e){
        }
    }

}
